<template>
	<view v-if="detailList.goods_name" class="detail">
		<!-- 轮播图区域 -->
		<swiper class="datail-list" indicator-dots="true" autoplay="true" interval="3000" circular="true">
			<swiper-item v-for="(item ,i) in detailList.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="detail-info-box">
			<!-- 商品价格 -->
			<view class="detail-info-price">
				￥{{detailList.goods_price}}
			</view>
			<!-- 商品主体区域 -->
			<view class="detail-info-body">
				<!-- 商品名称 -->
				<view class="detail-info-name">
					{{detailList.goods_name}}
				</view>
				<!-- 收藏 -->
				<view class="detail-shoucang">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			 <!-- 运费 -->
			<view class="yf">快递：免运费</view>
		</view>
		<!-- 商品详情信息 -->
		<rich-text :nodes="detailList.goods_introduce" class="datail-text"></rich-text>
		<view class="goods-carts">
			<uni-goods-nav 
			:options="options" 
			:fill="true" 
			:button-group="buttonGroup" 
			@click="onClick"
			@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
import {mapState,mapMutations,mapGetters} from 'vuex'

export default {
	//监听事件，监听购物车数量属性
	watch:{
		total(newVal){
			const number = this.options.find(item => item.text == '购物车')
			if(number){
				number.info = newVal
			}
		}
	},
	computed:{
		...mapState("m_cart",["cart"]),
		...mapGetters("m_cart",["total"])
	},
	data() {
		return {
			good_id: '',
			detailList: [],
			options: [{
				icon: 'shop',
				text: '店铺',
				infoBackgroundColor: '#007aff',
				infoColor: "#f5f5f5"
			}, {
				icon: 'cart',
				text: '购物车'
			}],
			buttonGroup: [{
					text: '加入购物车',
					backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
					color: '#fff'
				},
				{
					text: '立即购买',
					backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
					color: '#fff'
				}
			]
		};
	},
	onLoad(ops) {
		this.getDetailList(ops)
		this.good_id = ops
		this.totle()
	},
	methods: {
		...mapMutations("m_cart",["addToCart","getToStorage"]),
		async getDetailList(ops){
			const {data:res} = await uni.$http.get("/api/public/v1/goods/detail",ops)
			if (res.meta.status != 200) return uni.$showMessage()
			res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,'<img style="display:block;" ')
			this.detailList = res.message
		},
		//点击图片预览
		preview(i){
			uni.previewImage({
				current:i,
				urls:this.detailList.pics.map(item => item.pics_big)
			})
		},
		onClick(e) {
			if(e.index == 1){
				//跳转到购物车
				console.log(e.index)
				uni.switchTab({
					url:"/pages/cart/cart"
				})
			}
		},
		buttonClick(e) {
			if(e.index == 0){
				//点击加入购物车
				const goods = {
					goods_id: this.detailList.goods_id,       // 商品的Id
					goods_name: this.detailList.goods_name,   // 商品的名称
					goods_price: this.detailList.goods_price, // 商品的价格
					goods_count: 1,                           // 商品的数量
					goods_small_logo: this.detailList.goods_small_logo, // 商品的图片
					goods_state: true                         // 商品的勾选状态
				}
				this.addToCart(goods)
				
			}
		},
		totle(){
			const totle = JSON.parse(uni.getStorageSync('cart') || '[]')
			const number = this.options.find(item => item.text == '购物车')
			if(number){
				number.info = totle[0].goods_count
			}
		}
	},
	onPullDownRefresh() {
		this.getDetailList(this.good_id)
		uni.stopPullDownRefresh()
	}
}
</script>

<style lang="scss">
.datail-list{
	height: 750rpx;
	image{
		width: 100%;
		height: 100%;
	}
}
.detail-info-box{
	margin: 5px 5px;
	//价格
	.detail-info-price{
		color: red;
		font-size: 18px;
	}
	//主体区域
	.detail-info-body{
		display: flex;
		justify-content: space-between;
		//商品名称
		.detail-info-name{
			font-size: 12px;
			padding-right: 12px;
		}
		//商品收藏
		.detail-shoucang{
			display: flex;
			font-size: 12px;
			justify-content: center;
			border-left: 1px solid gray;
			flex-direction: column;
			align-items: center;
			color: gray;
			width: 120px;
		}
	}
}
.yf{
	margin: 10px 0;
	font-size: 12px;
	color: gray;
}
//购物车
.goods-carts {
	display: flex;
	flex-direction: column;
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
}
.detail{
	padding-bottom: 50px;
}
</style>
